﻿using EasyNet.Componets.Core.Excels;
using EasyNet.Componets.Core.Inject;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
    public class ExcelUtil
    {
        private IExcel _instance;

        private IExcel Instance
        {
            get
            {
                if (_instance != null)
                {
                    return _instance;
                }
                _instance = IoC.Resolve<IExcel>();

                return _instance;
            }
        }

        public ExcelUtil()
        {
            _instance = IoC.Resolve<IExcel>();
        }

        public ExcelUtil(IExcel instance)
        {
            _instance = instance;
        }

        #region Properties
        /// <summary>
        /// 当前操作的 SheetName
        /// </summary>
        public string CurrentSheetName { get { return _instance.CurrentSheetName; } }

        /// <summary>
        /// 当前操作的 SheetIndex
        /// </summary>
        public int CurrentSheetIndex { get { return _instance.CurrentSheetIndex; } }

        /// <summary>
        /// 当前操作的 RowIndex
        /// </summary>
        public int CurrentRowIndex { get { return _instance.CurrentRowIndex; } }

        /// <summary>
        /// 当前操作的 ColumnIndex
        /// </summary>
        public int CurrentColumnIndex { get { return _instance.CurrentColumnIndex; } }

        /// <summary>
        /// 激活的 SheetIndex
        /// </summary>
        public int ActiveSheetIndex { get { return _instance.ActiveSheetIndex; } }
        #endregion

        #region Workbook
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="file">文件名</param>
        public void Open(string file)
        {
            _instance.Open(file);
        }

        #endregion

        #region Sheet
        /// <summary>
        /// 激活Sheet
        /// </summary>
        /// <param name="sheetIndex">Sheet索引号：起始索引号从0开始</param>
        public void ActiveSheet(int sheetIndex)
        {
            _instance.ActiveSheet(sheetIndex);
        }

        /// <summary>
        /// 创建Sheet
        /// 被创建的Sheet将会成为当前操作的Sheet
        /// </summary>
        public void CreateSheet()
        {
            _instance.CreateSheet();
        }

        /// <summary>
        /// 创建Sheet
        /// 被创建的Sheet将会成为当前Sheet
        /// </summary>
        /// <param name="sheetName"></param>
        public void CreateSheet(string sheetName)
        {
            _instance.CreateSheet(sheetName);
        }

        /// <summary>
        /// 设置当前Sheet
        /// </summary>
        /// <param name="sheetName"></param>
        public void SetCurrentSheet(string sheetName)
        {
            _instance.SetCurrentSheet(sheetName);
        }

        /// <summary>
        /// 设置当前Sheet
        /// </summary>
        /// <param name="sheetIndex">Sheet索引号：起始从0开始</param>
        public void SetCurrentSheet(int sheetIndex)
        {
            _instance.SetCurrentSheet(sheetIndex);
        }

        /// <summary>
        /// 激活公式
        /// </summary>
        public void ActiveFormula()
        {
            _instance.ActiveFormula();
        }

        /// <summary>
        /// 禁用公式
        /// </summary>
        public void DisableFormula()
        {
            _instance.DisableFormula();
        }
        #endregion

        #region Row
        /// <summary>
        /// 设置当前Row
        /// </summary>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        public void SetCurrentRow(int rowIndex)
        {
            _instance.SetCurrentRow(rowIndex);
        }

        /// <summary>
        /// 设置当前Row
        /// </summary>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <param name="sheetIndex">Sheet 索引号：起始索引从0开始</param>
        public void SetCurrentRow(int rowIndex, int sheetIndex)
        {
            SetCurrentSheet(sheetIndex);

            SetCurrentRow(rowIndex);
        }

        /// <summary>
        /// 创建Row
        /// 被创建的Row会被设置成新的CurrentRow
        /// </summary>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        public void CreateRow(int rowIndex)
        {
            _instance.CreateRow(rowIndex);
        }

        /// <summary>
        /// 创建Row
        /// 被创建的Row会被设置成新的CurrentRow
        /// </summary>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <param name="sheetIndex">Sheet 索引号：起始索引从0开始</param>
        public void CreateRow(int rowIndex, int sheetIndex)
        {
            SetCurrentSheet(sheetIndex);

            CreateRow(rowIndex);
        }
        #endregion

        #region Cell
        /// <summary>
        /// 创建Cell
        /// 被创建的 Cell 会被设置成新的 CurrentCell
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        public void CreateCell(int columnIndex)
        {
            _instance.CreateCell(columnIndex);
        }

        /// <summary>
        /// 创建Cell
        /// 被创建的 Cell 会被设置成新的 CurrentCell
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        public void CreateCell(int columnIndex, int rowIndex)
        {
            SetCurrentRow(rowIndex);
            CreateCell(columnIndex);
        }

        /// <summary>
        /// 创建Cell
        /// 被创建的 Cell 会被设置成新的 CurrentCell
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <param name="sheetIndex">Sheet 索引号：起始索引从0开始</param>
        public void CreateCell(int columnIndex, int rowIndex, int sheetIndex)
        {
            SetCurrentSheet(sheetIndex);
            CreateCell(columnIndex, rowIndex);
        }

        /// <summary>
        /// 设置当前单元格
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        public void SetCurrentCell(int columnIndex)
        {
            _instance.SetCurrentCell(columnIndex);

        }

        /// <summary>
        /// 读取 当前单元格值
        /// </summary>
        /// <returns>单元格值</returns>
        public object ReadCell()
        {
            return _instance.ReadCell();
        }

        /// <summary>
        /// 读取 当前单元格值
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <returns>单元格值</returns>
        public object ReadCell(int columnIndex)
        {
            SetCurrentCell(columnIndex);

            return ReadCell();
        }

        /// <summary>
        /// 读取 当前单元格值
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <returns>单元格值</returns>
        public object ReadCell(int columnIndex, int rowIndex)
        {
            SetCurrentRow(rowIndex);

            return ReadCell(columnIndex);
        }

        /// <summary>
        /// 读取 当前单元格值
        /// </summary>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <param name="sheetIndex">Sheet索引号：起始索引从0开始</param>
        /// <returns>单元格值</returns>
        public object ReadCell(int columnIndex, int rowIndex, int sheetIndex)
        {
            SetCurrentSheet(sheetIndex);

            return ReadCell(columnIndex, rowIndex);
        }

        /// <summary>
        /// 写 当前单元格值
        /// </summary>
        /// <param name="obj">值对象</param>
        public void WriteCell(object obj)
        {
            _instance.WriteCell(obj);
        }

        /// <summary>
        /// 写 当前单元格值
        /// </summary>
        /// <param name="obj">值对象</param>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        public void WriteCell(object obj, int columnIndex)
        {
            SetCurrentCell(columnIndex);

            WriteCell(obj);
        }

        /// <summary>
        /// 写 当前单元格值
        /// </summary>
        /// <param name="obj">值对象</param>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        public void WriteCell(object obj, int columnIndex, int rowIndex)
        {
            SetCurrentRow(rowIndex);

            WriteCell(obj, columnIndex);
        }

        /// <summary>
        /// 写 当前单元格值
        /// </summary>
        /// <param name="obj">值对象</param>
        /// <param name="columnIndex">列索引号：起始索引从0开始</param>
        /// <param name="rowIndex">行索引号：起始索引从0开始</param>
        /// <param name="sheetIndex">Sheet索引号：起始索引从0开始</param>
        public void WriteCell(object obj, int columnIndex, int rowIndex, int sheetIndex)
        {
            SetCurrentSheet(sheetIndex);

            WriteCell(obj, columnIndex, rowIndex);
        }


        #endregion

        #region 设置公式
        /// <summary>
        /// 设置单元格 公式
        /// </summary>
        /// <param name="formula">公式</param>
        public void WriteCellFormula(string formula)
        {

            _instance.WriteCellFormula(formula);
        }

        #endregion

        #region Save
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="isReplace">是否替换</param>
        public void Save(string file, bool isReplace)
        {
            _instance.Save(file, isReplace);
        }

        public void Save(Stream stream)
        {
            _instance.Save(stream);
        }

        public byte[] Save()
        {
            return _instance.Save();
        }
        #endregion
    }
}
